package com.ruoyi.common.utils.msg.util.format;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TimeUtils {
    /**
     * 判断两个时间字符串是否在同一小时内
     * 支持不同格式：
     *   - 14位：yyyyMMddHHmmss
     *   - 12位：yyyyMMddHHmm
     */
    public static boolean isSameHour(String timeStr1, String timeStr2) {
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyyMMddHHmm");

        LocalDateTime t1 = parseTime(timeStr1, formatter1);
        LocalDateTime t2 = parseTime(timeStr2, formatter2);

        if (t1 == null || t2 == null) {
            return false; // 格式错误或无法解析
        }

//        Boolean year = t1.getYear() == t2.getYear();
//        Boolean month = t1.getMonthValue() == t2.getMonthValue();
//        Boolean day = t1.getDayOfMonth() == t2.getDayOfMonth();
//        Boolean hour = t1.getHour() == t2.getHour();
//        Boolean hour1 = isWithinOneHour(timeStr1, timeStr2);
        return t1.getYear() == t2.getYear()
            && t1.getMonthValue() == t2.getMonthValue()
            && t1.getDayOfMonth() == t2.getDayOfMonth()
            &&  isWithinOneHour(timeStr1, timeStr2);
    }

    /**
     * 判断两个时间是否在“同一小时内”，允许相差不超过 1 小时
     */
    public static boolean isWithinOneHour(String timeStr1, String timeStr2) {
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyyMMddHHmm");

        LocalDateTime t1 = parseTime(timeStr1, formatter1);
        LocalDateTime t2 = parseTime(timeStr2, formatter2);

        if (t1 == null || t2 == null) {
            return false;
        }

        return Duration.between(t1, t2).abs().getSeconds() <= 3600;
    }

    private static LocalDateTime parseTime(String timeStr, DateTimeFormatter formatter) {
        try {
            return LocalDateTime.parse(timeStr, formatter);
        } catch (Exception e) {
            // 新增对 "yyyy-MM-dd HH:mm:ss" 的支持
            try {
                DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                return LocalDateTime.parse(timeStr, newFormatter);
            } catch (Exception ex) {
                System.err.println("时间解析失败: " + timeStr);
                return null;
            }
        }
//        try {
//            return LocalDateTime.parse(timeStr, formatter);
//        } catch (Exception e) {
//            System.err.println("时间解析失败: " + timeStr);
//            return null;
//        }
    }

    public static String formatStandard(String timeStr) {
        if (timeStr.length() == 12) {
            return timeStr.substring(0, 4) + "-" + timeStr.substring(4, 6) + "-" +
                timeStr.substring(6, 8) + " " + timeStr.substring(8, 10) + ":" +
                timeStr.substring(10, 12) + ":00";
        }
        return timeStr; // 默认返回原值或做其他处理
    }

    public static String formatStandardWithOneHourAdded(String timeStr) {
        if (timeStr == null || timeStr.isEmpty()) {
            System.err.println("时间解析失败: 输入为空");
            return null;
        }

        if (timeStr.length() == 12) {
            try {
                DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
                DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

                LocalDateTime parsed = LocalDateTime.parse(timeStr, inputFormatter);
                LocalDateTime plusOneHour = parsed.plusHours(1);

                return plusOneHour.format(outputFormatter);
            } catch (Exception e) {
                System.err.println("时间解析失败: " + timeStr);
                return null;
            }
        } else {
            System.err.println("时间解析失败: 不支持的格式（需为12位时间字符串）");
            return null;
        }
    }

}
